package com.dks.repository.impl;

import java.util.*;

import com.dks.mapper.DoctorMapper;
import com.dks.mapper.TalksRecordMapper;
import com.dks.models.RecordDate;
import com.dks.mapper.RecordDateMapper;
import com.dks.models.TalksRecord;
import com.dks.repository.IRecordDateRepository;
import com.dks.result.ResultModel;
import com.dks.util.Commond;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import static com.dks.util.Commond.MSG_SUCCESS;

@Repository
public class RecordDateRepository implements IRecordDateRepository {

    @Resource
    private RecordDateMapper recordDateMapper;

    @Resource
    private TalksRecordMapper talksRecordMapper;


    @Override
    public ResultModel<Boolean> insert(RecordDate recordDate) {
        ResultModel resultModel = new ResultModel();
        int row = recordDateMapper.insert(recordDate);
        if (1 == row) {
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(Commond.MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage(Commond.MSG_FALE);
            resultModel.setData(row);
        }
        return resultModel;
    }

    @Override
    public ResultModel<Boolean> update(RecordDate recordDate) {
        ResultModel resultModel = new ResultModel();

        //查找当前时间预约人数
        Map map = new HashMap();
        map.put("recordTimeLike", recordDate.getRecordTime());
        List<TalksRecord> talksRecords = talksRecordMapper.selectList(map);
        int counts = talksRecords.size();
        //已预约人数比修改后人数少可以修改，否则提示不能修改
        if (counts < recordDate.getPersonCount()) {
            int row = recordDateMapper.update(recordDate);
            resultModel.setCode(Commond.CODE_SUCCESS);
            resultModel.setMessage(Commond.MSG_SUCCESS);
            resultModel.setData(row);
        } else {
            resultModel.setCode(Commond.CODE_FAIL);
            resultModel.setMessage("修改后人数不能小于已预约人数");
            resultModel.setData(0);
        }
        return resultModel;
    }

    @Override
    public ResultModel<Boolean> delete(Integer id) {
        return null;
    }

    @Override
    public ResultModel<Boolean> select(Integer id) {
        ResultModel resultModel = new ResultModel();
        RecordDate recordDate = recordDateMapper.select(id);
        resultModel.setCode(Commond.CODE_SUCCESS);
        resultModel.setMessage(MSG_SUCCESS);
        resultModel.setData(recordDate);

        return resultModel;
    }

    @Override
    public ResultModel<Boolean> selectByList() {
        Map map = new HashMap();
        ResultModel resultModel = new ResultModel();
        List<RecordDate> recordDates = recordDateMapper.getByMapList(map);
        resultModel.setCode(Commond.CODE_SUCCESS);
        resultModel.setMessage(MSG_SUCCESS);
        resultModel.setData(recordDates);
        return resultModel;
    }

    @Override
    public Integer cntByMap(Map<String, Object> map) {
        return recordDateMapper.cntByMap(map);
    }

    @Override
    public List<Integer> getByMap(Map<String, Object> map) {
        return recordDateMapper.getByMap(map);
    }

    @Override
    public List<RecordDate> getByMapList(Map<String, Object> map) {
        return recordDateMapper.getByMapList(map);
    }


}
